import banners from "../../../modules/banner";
import { Router } from "express";

const router = Router();

router.get("/banner", async (req, res: any) => {
  try {
    const result = await banners.findAll({ where: { isdeleted: false } });
    if (result) {
      res.send({
        status: 200,
        message: "Success",
        data: result,
      });
    } else {
      res.cc("数据库查询出错");
    }
  } catch (error) {
    res.cc(error);
  }
});
router.get("/banner/:id", async (req, res: any) => {
  const { id } = req.params;
  try {
    const result = await banners.findOne({ where: { id } });
    if (result) {
      res.send({
        status: 200,
        message: "Success",
        data: result,
      });
    } else {
      res.cc("数据库查询出错");
    }
  } catch (error) {
    res.cc(error);
  }
});
router.post("/banner", async (req, res: any) => {
  const { title, url } = req.body;
  try {
    const result = await banners.create({ title, url });
    if (result) {
      res.send({
        status: 200,
        message: "Success",
        data: result,
      });
    } else {
      res.cc("数据库查询出错");
    }
  } catch (error) {
    res.cc(error);
  }
});
router.put("/banner", async (req, res: any) => {
  const { id, title, url } = req.body;
  try {
    const result = await banners.update({ title, url }, { where: { id } });
    if (result) {
      res.send({
        status: 200,
        message: "Success",
        data: result,
      });
    } else {
      res.cc("数据库查询出错");
    }
  } catch (error) {
    res.cc(error);
  }
});
router.delete("/banner", async (req, res: any) => {
  const { id } = req.body;
  try {
    const result = await banners.update({ isdeleted: true }, { where: { id } });
    if (result) {
      res.send({
        status: 200,
        message: "Success",
        data: result,
      });
    } else {
      res.cc("数据库查询出错");
    }
  } catch (error) {
    res.cc(error);
  }
});
export default router;
